package com.API.beans;

import com.API.dal.entities.*;
import com.API.dal.providers.CostsProvider;
import com.API.dal.providers.EquipmentsProvider;
import com.API.dal.providers.ProjectProvider;
import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

@Component
@Scope("request")
public class MechanismsBean {

    @Autowired
    EquipmentsProvider equipmentsProvider;
    @Autowired
    ProjectProvider projectProvider;
    @Autowired
    CostsProvider costsProvider;

    List<Equipments> equipmentsList;
    Equipments equipments;
    Equipments selectedMechanism;
    Objects project;
    EquipmentsProjects equipmentsProjects;

    public MechanismsBean(){
        equipments = new Equipments();
        project = new Objects();
        equipmentsProjects = new EquipmentsProjects(new EquipmentsProjectsId(),
                new Objects(), new Equipments());

    }

    public EquipmentsProjects getEquipmentsProjects() {
        return equipmentsProjects;
    }

    public void setEquipmentsProjects(EquipmentsProjects equipmentsProjects) {
        this.equipmentsProjects = equipmentsProjects;
    }

    public Objects getProject() {
        return project;
    }

    public void setProject(Objects project) {
        this.project = project;
    }

    public Equipments getEquipments() {
        return equipments;
    }

    public void setEquipments(Equipments equipments) {
        this.equipments = equipments;
    }

    public void setMechanism(Equipments equipments){
        this.equipments = equipments;
    }
    public Equipments getMechanism(){
        return this.equipments;
    }

    public Equipments getSelectedMechanism() {
        return selectedMechanism;
    }

    public void setSelectedMechanism(Equipments selectedMechanism) {
        this.selectedMechanism = selectedMechanism;
    }


    public List<Equipments> getEquipmentsList(){
        if (equipmentsList == null)
            equipmentsList = equipmentsProvider.getMechanisms();
        return equipmentsList;
    }

    public List<String> getMechanismComplete(String query){
        List<String> strList = new LinkedList<String>();
        for (Equipments eq: equipmentsProvider.getMechanisms()){
            String str = eq.getName();
            if (str.startsWith(query) || str.startsWith(query.toUpperCase())
                    || str.startsWith(query.toLowerCase())){
                strList.add(str);
            }
        }
        return strList;
    }


    public void addMechanism(){
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("addMechanismDialog.show()");

    }

    public void saveAdd(){
            RequestContext context = RequestContext.getCurrentInstance();
            context.update("mechanismForm");
            Units unit = equipmentsProvider.getEquipmentUnit("шт");
            EquipmentsTypes equipmentsTypes = equipmentsProvider.getEquipmentType("Техника");
            equipments.setEquipmentsTypes(equipmentsTypes);
            equipments.setUnits(unit);
            equipmentsProvider.addMaterial(equipments);
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Техника добавлена успешно", equipments.getName());
            FacesContext.getCurrentInstance().addMessage(null, msg);
            context.addCallbackParam("saveAdd", true);

    }


    public void edit(){
        final RequestContext context = RequestContext.getCurrentInstance();

        if (selectedMechanism != null){
            selectedMechanism = equipmentsProvider.getEquipmentById(selectedMechanism.getId());
            context.update("editMechanismDialog.update()");
            context.execute("editMechanismDialog.show()");
        }
        else {
            context.update("mechanismForm");
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Ошибка.","Техника не выбрана");
            FacesContext.getCurrentInstance().addMessage(null, msg);

        }
    }

    public void closeEdit(){
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("editMechanismDialog.hide()");
    }

    public void saveEdit(){
        RequestContext context = RequestContext.getCurrentInstance();
        context.update("mechanismForm");
        Units unit = equipmentsProvider.getEquipmentUnit("шт");
        EquipmentsTypes equipmentsTypes = equipmentsProvider.getEquipmentType("Техника");
        selectedMechanism.setEquipmentsTypes(equipmentsTypes);
        selectedMechanism.setUnits(unit);
        equipmentsProvider.updateEquipment(selectedMechanism);
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                "Техника отредактирована успешно", selectedMechanism.getName());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("saveEdit", true);
    }

    public void coming(){
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("comingMechanismDialog.show()");

    }

    public void saveComing(){
        RequestContext context = RequestContext.getCurrentInstance();
        context.update("mechanismForm");
        equipments = equipmentsProvider.getEquipmentByName(equipmentsProjects.getEquipments().getName());
        Objects project = projectProvider.getProjectByName(equipmentsProjects.getObjects().getName());
        equipmentsProjects.getId().setProjectId(project.getId());
        equipmentsProjects.getId().setEquipmentId(equipments.getId());
        double sum = equipmentsProjects.getId().getTotal()*equipmentsProjects.getId().getPrice().doubleValue();
        equipmentsProjects.getId().setTotalSum(BigDecimal.valueOf(sum));
        equipmentsProjects.setObjects(project);
        equipmentsProjects.setEquipments(equipments);
        equipmentsProvider.addMaterialProject(equipmentsProjects);
        EquipmentCosts eq = new EquipmentCosts();
        Costs cost = new Costs();
        cost.setCostsTypes(costsProvider.getCostTypeByName("Заказ техники"));
        cost.setChangeDate(equipmentsProjects.getId().getDeliveryDate());
        cost.setTotal(BigDecimal.valueOf(sum));
        cost.setNotes("Работа техники на объекте " + project.getName());
        costsProvider.addCost(cost);
        eq.setCosts(cost);
        eq.setEquipments(equipments);
        eq.getId().setCostId(cost.getId());
        eq.getId().setEquipmentId(equipments.getId());
        equipmentsProvider.addEquipmentCost(eq);
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                equipments.getName(), "Работа техники проведена успешно." );
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("saveComing", true);

    }

    public void closeComing(){
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("comingMechanismDialog.hide()");
    }




}
